package com.zhoug.common3.screenrecord;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;

import com.zhoug.common3.content.SimpleBinder;
import com.zhoug.common3.utils.LogUtils;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

/**
 * 录屏服务
 *
 * @Author: zhoug
 * @Date: 2024-09-03
 * @Description:
 */
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class ScreenRecordService extends Service {
    private static final String TAG = ">>>ScreenRecordService";
    private static final String PARAM_RESULT_CODE = "resultCode";
    private static final String PARAM_RESULT_DATA = "resultData";
    private static final String PARAM_KEEP_PATH = "keepPath";
    private static final String PARAM_NOTIFICATION_ICON = "notificationIcon";

    private IBinder mBinder;
    private ScreenRecordUtils mScreenRecordUtils;

    /**
     * 服务启动工具
     */
    public static final class Launcher {
        private final Intent service;
        private final Context context;


        public Launcher(Context context) {
            this.context = context;
            this.service = new Intent(this.context, ScreenRecordService.class);
        }

        public Launcher setResultCode(int resultCode) {
            service.putExtra(PARAM_RESULT_CODE, resultCode);
            return this;
        }

        public Launcher setResultData(Intent resultData) {
            service.putExtra(PARAM_RESULT_DATA, resultData);
            return this;
        }

        public Launcher setKeepPath(String keepPath) {
            service.putExtra(PARAM_KEEP_PATH, keepPath);
            return this;
        }

        public void start() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startForegroundService(service);
            } else {
                context.startService(service);
            }
        }
    }

    /**
     * 停止录屏
     *
     * @param context
     */
    public static void stop(Context context) {
        Intent service = new Intent(context, ScreenRecordService.class);
        context.stopService(service);
    }


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        LogUtils.d(TAG, "onBind:");
        if (mBinder == null) {
            mBinder = new SimpleBinder<>(this);
        }
        return mBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        LogUtils.d(TAG, "onCreate:");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        LogUtils.d(TAG, "onStartCommand:");
        try {
            int resultCode = 0;
            Intent resultData = null;
            String videoKeepPath = null;//视频保存地址
            if (null != intent) {
                resultCode = intent.getIntExtra(PARAM_RESULT_CODE, 0);
                resultData = intent.getParcelableExtra(PARAM_RESULT_DATA);
                videoKeepPath = intent.getStringExtra(PARAM_KEEP_PATH);
            }
            //启动前台服务通知
            startNotification();
            //开始录屏
            mScreenRecordUtils = new ScreenRecordUtils(getApplicationContext());
            mScreenRecordUtils.setScreenCaptureIntentResult(resultCode, resultData);
            mScreenRecordUtils.setVideoKeepPath(videoKeepPath);
            mScreenRecordUtils.startRecorder();
            ScreenRecordManager.sendStart(getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
            ScreenRecordManager.sendError(getApplicationContext(), e.getMessage());
            clearNotification();
            stopSelf();
        }
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * 启动前台服务通知
     */
    private void startNotification() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            LogUtils.d(TAG, "startNotification");
            IScreenRecordNotification screenRecordNotification = ScreenRecordManager.getScreenRecordNotification();
            Notification notification = screenRecordNotification.create(getApplicationContext());
            startForeground(screenRecordNotification.getNotificationId(), notification);
        }
    }

    /**
     * 清除前台通知
     */
    private void clearNotification() {
        NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager != null) {
            IScreenRecordNotification screenRecordNotification = ScreenRecordManager.getScreenRecordNotification();
            notificationManager.cancel(screenRecordNotification.getNotificationId());
        }
    }


    @Override
    public boolean onUnbind(Intent intent) {
        LogUtils.d(TAG, "onUnbind:");
        stopRecord();
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        LogUtils.d(TAG, "onDestroy:");
        stopRecord();
        super.onDestroy();
    }

    /**
     * 停止录屏
     */
    private void stopRecord() {
        if (mScreenRecordUtils != null && mScreenRecordUtils.isRecording()) {
            mScreenRecordUtils.stopRecord();
            ScreenRecordManager.sendStop(getApplicationContext(), mScreenRecordUtils.getVideoKeepPath());
        }
        mScreenRecordUtils = null;

        clearNotification();
    }

}
